package com.jokul.web.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jokul.model.User;
import com.jokul.service.UserService;
import com.jokul.service.UserServiceImpl;
import com.jokul.utils.MD5;

/**
 *
 *@author jokul
 *@2013-3-30 
 *@下午8:30:31
 */
@WebServlet("/login")
public class Login extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	private static final String SUCCESS = "/pages/user/member.jsp";
	private static final String ERROR = "index.jsp";
	
	private UserService userService = new UserServiceImpl();

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		
		User user = userService.getUser(userName);
		req.setAttribute("userName", req.getParameter("userName"));
		if(MD5.getMD5(password).equals(user.getPassword())) {
			HttpSession session = req.getSession();
			session.setAttribute("userId", user.getUserId());
			req.getRequestDispatcher(SUCCESS).forward(req, resp);
		}else {
			resp.sendRedirect(ERROR);
		}
	}
}
